package oocl.ita.eordering.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import oocl.ita.eordering.dao.FoodDao;
import oocl.ita.eordering.po.Food;
import oocl.ita.eordering.service.FoodService;
import oocl.ita.eordering.util.UUIDUtil;

@Service("fs")
public class FoodServiceImpl implements FoodService {
	@Autowired
	private FoodDao fd;

	@Override
	@Transactional
	public int addFood(Food f) {
		// TODO Auto-generated method stub
		return fd.addFood(f);
	}

	@Override
	public List<Food> findFoodsByMId(Integer mId) {
		// TODO Auto-generated method stub
		return fd.findFoodsByMId(mId);
	}

	@Override
	@Transactional
	public boolean deleteFood(Integer fId) {
		// TODO Auto-generated method stub
		if (fd.deleteFood(fId)==1) {
			return true;
		}else{
			return false;
		}
	}

	@Override
	@Transactional
	public int updateFood(Integer fId, String fName, double fPrice, String fViewPath) {
		// TODO Auto-generated method stub
		return fd.updateFood(fId,fName,fPrice,fViewPath);
	}

	@Override
	public String getFoodViewPath(String mTel, MultipartFile foodPic) {
		File dir = null;
		String fViewPath = null;
		
		String fileName = foodPic.getOriginalFilename();
		if (fileName.lastIndexOf(".") == -1) {
			System.out.println("文件格式错误！！！");
		}
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		
		dir = new File("D:/MerchantUploadFiles/" + mTel + "/Foods");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		fViewPath = mTel + "/Foods/" + UUIDUtil.getUuid() + suffix;
		
		try {
			foodPic.transferTo(new File("D:/MerchantUploadFiles/" + fViewPath));
		} catch (IllegalStateException e) {
			System.out.println("IllegalStateException...文件保存本地错误");
			//return "errorPage";
		} catch (IOException e) {
			System.out.println("IOException...文件保存本地错误");
			//return "errorPage";
		}
		
		return fViewPath;
	}

	@Override
	@Transactional
	public boolean updateStatus(Integer fId) {
		// TODO Auto-generated method stub
		return fd.updateStatus(fId);
	}

	

	

}
